package com.map;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.content.Context;
import android.graphics.*;
import android.location.*;
import android.os.Bundle;
import android.util.*;
import android.view.*;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.Toast;
 
public class GMapsActivity extends MapActivity {
	   MapView mapView; 
	   private MapController mc;
	   private LocationManager lm;
	   GeoPoint p;
	    private LocationListener locationListener;
	    
	    
	    class MapOverlay extends com.google.android.maps.Overlay
	    {
	        
	    	 @Override
	         public boolean onTouchEvent(MotionEvent event, MapView mapView) 
	         {   
	             //---when user lifts his finger---
	             if (event.getAction() == 1) {                
	                 GeoPoint p = mapView.getProjection().fromPixels(
	                     (int) event.getX(),
	                     (int) event.getY());
	  
	                 Geocoder geoCoder = new Geocoder(
	                     getBaseContext(), Locale.getDefault());
	                 try {
	                     List<Address> addresses = geoCoder.getFromLocation(
	                         p.getLatitudeE6()  / 1E6, 
	                         p.getLongitudeE6() / 1E6, 1);
	  
	                     String add = "";
	                     if (addresses.size() > 0) 
	                     {
	                         for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
	                              i++)
	                            add += addresses.get(0).getAddressLine(i) + "\n";
	                     }
	  
	                     Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
	                 }
	                 catch (IOException e) {                
	                     e.printStackTrace();
	                 }   
	                 return true;
	             }
	             else                
	                 return false;
	         }        
	    } 
	    
	    
	    
    /** Called when the activity is first created. */    
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapView = (MapView) findViewById(R.id.mapView);
        mc = mapView.getController();
        String coordinates[] = {"1.352566007", "103.78921587"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
 
        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));
 
        mc.animateTo(p);
        //---use the LocationManager class to obtain GPS locations---
        Log.i("FONZI","<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<DENTRO1>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    
        Log.i("FONZI","<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<DENTRO2>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        locationListener = new MyLocationListener();
        Log.i("FONZI","<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<DENTRO3>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        lm.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);
        Log.i("FONZI","<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<DENTRO4>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
        View zoomView = mapView.getZoomControls(); 
 
        zoomLayout.addView(zoomView, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);
        
        
      //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);    
        
        
    }
 
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    
    private class MyLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location loc) {
            Log.i("FONZI","<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<DENTRO5>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

            if (loc != null) {                
                Log.i("FONZI","<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<DENTRO6>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

                Toast.makeText(getBaseContext(), 
                    "Location changed : Lat: " + loc.getLatitude() + 
                    " Lng: " + loc.getLongitude(), 
                    Toast.LENGTH_SHORT).show();
                
                GeoPoint p = new GeoPoint(
                        (int) (loc.getLatitude() * 1E6), 
                        (int) (loc.getLongitude() * 1E6));
                mc.animateTo(p);
                mc.setZoom(16);                
                mapView.invalidate();
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStatusChanged(String provider, int status, 
            Bundle extras) {
            // TODO Auto-generated method stub
        }
    }    
}
